/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/
/*
Authors: Ram Ovadia 201374022 and Lior Fass 300069705
Project: HW Assignment 4 
Description: This is the implementation of the library of common functionality in Oiway.  
*/
/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/

#include "common_utils.h"


int winsock_init()
{
	WSADATA wsa;
	//DEBUG:
	printf("\nInitialising Winsock...\r\n");
	if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) //(WSAStartup(MAKEWORD(2,2), &(client->wsa)) != 0)
	{
		printf("Failed. Error Code : %d\r\n",WSAGetLastError());
		return 1;
	}
	//DEBUG:
	printf("Winsock Initialised.\r\n");
	return 0;
}

BOOL CreateProcessSimple( LPTSTR CommandLine, PROCESS_INFORMATION *ProcessInfoPtr )
{
	STARTUPINFO	startinfo = { sizeof( STARTUPINFO ), NULL, 0 }; /* <ISP> here we */
	/* initialize a "Neutral" STARTUPINFO variable. Supplying this to */
	/* CreateProcess() means we have no special interest in this parameter. */
	/* This is equivalent to what we are doing by supplying NULL to most other */
	/* parameters of CreateProcess(). */ 

	return CreateProcess( NULL, /*  No module name (use command line). */
		CommandLine,			/*  Command line. */
		NULL,					/*  Process handle not inheritable. */
		NULL,					/*  Thread handle not inheritable. */
		FALSE,					/*  Set handle inheritance to FALSE. */
		CREATE_NEW_CONSOLE,		/*  creation/priority flags. */
		NULL,					/*  Use parent's environment block. */
		NULL,					/*  Use parent's starting directory. */
		&startinfo,				/*  Pointer to STARTUPINFO structure. */
		ProcessInfoPtr			/*  Pointer to PROCESS_INFORMATION structure. */
		);
}

void writeToLog(FILE *file, char *buf)
{
	printf("%s", buf);
	fprintf(file, "%s", buf);
}

void writeLogAndScreen(FILE *file,const char * format,...)
{
	char buffer[MAX_LINE_LENGTH];
	va_list args;
	va_start (args, format);
	vsnprintf (buffer, MAX_LINE_LENGTH-1, format, args);
	va_end (args);
	
	printf(buffer);
	fprintf(file,buffer);
}

BOOL isInteger(char *str)
{
	int i, length = 0;

	length = strlen(str);
	for (i=0; i < length; i++)
	{
		if ( !( (str[i] > 47) && (str[i] < 58) ) )
		{
			return FALSE;
		}
	}
	return TRUE;
}

int main_test_input(int argc, char *argv[])
{
	unsigned long address;

	if (( argc != 6 ) && ( argc != 8 ))
	{
		printf("wrong number of arguments");
		return 1;
	}

	if ( strcmp(argv[ADDRESS_ARG], LOCAL_HOST_S) != 0 )
	{
		address = inet_addr( argv[ADDRESS_ARG] );
		if (( address == INADDR_NONE ) || ( address == INADDR_ANY ))
		{
			printf("The string \"%s\" is not a valid ip address\r\n", argv[ADDRESS_ARG]);
			return 1;
		}
	}

	if ((argc == 6) && (strcmp((const char *)argv[SIDE_ARG], SERVER_S) == 0))
	{
		if (( isInteger(argv[S_MAXC_ARG]) ) && ( atoi(argv[S_MAXC_ARG]) > 0 ))
		{
			return 0;
		}

		printf("bad max number of clients argument\r\n");
		return 1;

	} 

	if ( (argc == 8) && ( strcmp((const char *)argv[SIDE_ARG], CLIENT_S) == 0 ))
	{
		return 0;
	}
			
	printf("bad server/client argument\r\n");
	return 1;
}